function SimpleToolBar(){
	HBox.apply(this,[]);	
	this.htmlX.addClass("ToolBar col-sm-offset-0 col-sm-12");
	this.htmlX.css({"marginBottom":"10px","z-index":"13"});
	/*
	<ul class="nav nav-pills">
	  <li class="active"><a href="#">Home</a></li>
	  <li><a href="#">Profile</a></li>
	  <li><a href="#">Messages</a></li>
	</ul>
*/
	this.htmlX.append('<div class="btn-group btn-group-justified"></div>');
	this.addButton = function(nbuttom,resize){
		//nbuttom.htmlX.addClass('navbar-btn');
		//var tmplink = $('<li>');
		//tmplink.append(nbuttom.htmlX);
	    this.htmlX.find("div.btn-group").append(nbuttom.htmlX);	
	};
}	

function ToolBar(pconfig){	
	this._assoc = [];
	this.config ={
			"puid":""
			,"entity":pconfig.entity
			,"idEntity":"id"+pconfig.entity
			,"grid":"grid"
			,"act":["add","del","edit","reload","print"]
			,"service":[pconfig.entity+".add",pconfig.entity+".excluir",pconfig.entity+".editar",pconfig.entity+".get",pconfig.entity+".get"]
			,"url":["ws","ws","ws","ws","ws"]
			,"format":["json","json","json","json","json"]
			,"params":[[],[],[],[],[]]
			,"dbmap":[]
		};
	if(pconfig.grid){
		//eh obrigatorio o entity para uso dos services
		this.config["grid"] = pconfig.grid;
	}
	
	
	if(pconfig.dbmap){
		//eh obrigatorio o entity para uso dos services
		this.config["dbmap"] =  $.extend(pconfig.dbmap,[]);
		var servs = ["add","del","edit","reload","print"];
		for(var z = 0;z<5;z++){
			if(pconfig[servs[z]]){
				this.config.service[z] = pconfig[servs[z]];
			};
		}		
	}
	
	
	this.changeE = false;
	this.btAdd = new Button("Novo");
	this.btDel = new Button("Excluir");
	this.btSave = new Button("Salvar");
	//this.btCancel = new LinkButton("Cancelar");
	this.btReload = new Button("Carregar");
	//this.btPrint = new LinkButton("Imprimir");
	
	this.btAdd.setIcon("plus");
	this.btDel.setIcon("minus-sign");
	this.btSave.setIcon("floppy-disk");
	//this.btCancel.setIcon("icon-ban-circle");
	this.btReload.setIcon("refresh");
	//this.btPrint.setIcon("icon-print");	
	SimpleToolBar.apply(this,[]);	
	this.htmlX[0]["dta"] = this;
	
	this.btAdd.htmlX.click($.proxy(this.add, this));	
	this.btSave.htmlX.click($.proxy(this.save, this));
	this.btDel.htmlX.click($.proxy(this.del, this));
	//this.btCancel.htmlX.click($.proxy(this.cancel, this));
	this.btReload.htmlX.click($.proxy(this.reload, this));
	//this.btPrint.htmlX.click($.proxy(this.print, this));
	
	if(!pconfig["add"]){
		this.btAdd.setEnable(false);
	}
	if(!pconfig["del"]){
		this.btDel.setEnable(false);
	}
	if(!pconfig["add"]&&!pconfig["edit"]){
		this.btSave.setEnable(false);
	}
	if(!pconfig["reload"]){
		this.btReload.setEnable(false);
	}
	
	this.addButton(this.btAdd,false);
	this.addButton(this.btDel,false);
	this.addButton(this.btSave,false);
	//this.addButton(this.btCancel,false);
	this.addButton(this.btReload,false);
	//this.addButton(this.btPrint,true);		
}

ToolBar.prototype.addAssociation = function(passoc){	
	var configs = $.extend({"obj":"","mod":"","url":"","act":"","p":[],"puid":"","column":this.config.dbmap[0]["column"]},passoc);
	this._assoc[this._assoc.length]=configs;	
};
ToolBar.prototype.setConfig = function(pconfig){
	var advancedConfig = {
		"entity":pconfig.entity
		,"idEntity":"id"+pconfig.entity
		,"grid":"grid"
		,"act":["add","del","edit","reload","print"]
		,"service":[pconfig.entity+".add",pconfig.entity+".excluir",pconfig.entity+".editar",pconfig.entity+".get",pconfig.entity+".get"]
		,"url":["ws","ws","ws","ws","ws"]
		,"format":["json","json","json","json","json"]
		,"params":[[],[],[],[],[]]
		};
	this.config = $.extend(advancedConfig, pconfig);
};
ToolBar.prototype.setAttrConfig = function(pAttr,pvl){	
	this.config[pAttr] = pvl;
};
ToolBar.prototype.setActConfig = function(pactconfig){
	var indS = this.getIndexConfigAct(pactconfig.act);
	var nObAct = $.extend({"service":this.config["service"][indS],"url":this.config["url"][indS],"format":this.config["format"][indS]}, pactconfig);
	this.config["service"][indS]=nObAct.service;
	this.config["url"][indS]=nObAct.url;
	this.config["format"][indS]=nObAct.format;	
	this.config["params"][indS]=$.extend([],nObAct.params);	
};
ToolBar.prototype.setActAttrConfig = function(act,attr,pvl){
	if($.isArray(act)){
		var tma = act.length;
		for(var i=0;i<tma;i++){
			var indS = this.getIndexConfigAct(act[i]);
			this.config[attr][indS]=pvl;
		}
	}else{
		var indS = this.getIndexConfigAct(act);
		this.config[attr][indS]=pvl;
	}
};
ToolBar.prototype.getIndexConfigAct = function(pact){
	var indS = 0;
	for(var ix = 0;ix<5;ix++){
		if(this.config["act"][ix]==pact){
			indS=ix;
			break;
		}
	}
	return indS;
};
ToolBar.prototype.getConfigAct = function(pact){
	var indS = this.getIndexConfigAct(pact);
	
	if(this.config["puid"]==""){		
		var tela = this.htmlX.parents(".windowC")[0]["dta"];
		this.config["puid"] = tela._fnName;
	}
	
	return {"params":this.config["params"][indS],"puid":this.config["puid"],"act":pact,"service":this.config["service"][indS],"url":this.config["url"][indS],"format":this.config["format"][indS]};
};

ToolBar.prototype.setModoNormal = function(){
	this.btAdd.setEnable(true);
	this.btSave.setEnable(true);
	this.btDel.setEnable(true);
	//this.btCancel.setEnable(false);
	this.btReload.setEnable(true);
	//this.btPrint.setEnable(true);
};


ToolBar.prototype.ativarOpacao = function(bt){
	this.htmlX.find("li").removeClass("active");
	bt.htmlX.addClass("active");	
};

ToolBar.prototype.add = function(){
	if(!this.btAdd.htmlX.hasClass("Disabled")){
		this.btAdd.setEnable(false);
		this.btSave.setEnable(true);
		this.btDel.setEnable(false);
		//this.btCancel.setEnable(true);
		this.btReload.setEnable(false);
		//this.btPrint.setEnable(false);
		this.ativarOpacao(this.btAdd);
		if(this.config.entity!=""){
			var tela = this.htmlX.parents(".windowC")[0]["dta"];		
			tela[this.config.dbmap[0].field].setValue("");
			if(this.config.dbmap.length>0){
				tela[this.config.dbmap[0].field].htmlX.focus();
			}
		}
	}
};

ToolBar.prototype.del = function(){
	if(!this.btDel.htmlX.hasClass("Disabled")){
		this.setModoNormal();
		this.ativarOpacao(this.btDel);
		var tela = this.htmlX.parents(".windowC")[0]["dta"];
		var gridD = tela[this.config.grid];	
		var actDel = this.getConfigAct("del");
		if(actDel["service"]!=""){				
	            var mybojS = $.extend({},gridD.getSelectedItem(false));
	            mybojS["s"] = actDel["service"];
				if(actDel["puid"]!=""){
					mybojS["puid"] = actDel["puid"];
				}				
				$.each(this.config.dbmap, function(index, value) {
					if(value["fixed"]){
						mybojS[value.field] = tela[value.field].getValue();
					}					
				});
	            mybojS["url"] = actDel["url"];
	            mybojS["format"] = actDel["format"];	
	            var tmparms = actDel["params"].length;
	            if(tmparms > 0){
	            	if(tmparms > 1){
	            		mybojS["p"] = actDel["params"];
	            	}else{
	            		mybojS["p"] = actDel["params"][0];
	            	}	            	 
	            }	            
	            rm.addRequest(mybojS);
		}	
		gridD.removeItem();	
	}
};

ToolBar.prototype.sync = function(pgridd,ptela){
	var itemSA = pgridd.getSelectedItem(true);
	var tmlD = this.config.dbmap.length;
	for(var x = 0;x<tmlD;x++){
		var tmpP = this.config.dbmap[x];
		itemSA[tmpP.column]=ptela[tmpP.field].getValue();
	}
	/*
	$.each(this.config.dbmap, function(index, value) {
		itemS[value.column]=ptela[value.field].getValue();
	});	
	*/			
	pgridd.updateItem(itemSA);
	return $.extend({},itemSA);
};

ToolBar.prototype.save = function(){
	if(!this.btSave.htmlX.hasClass("Disabled")){
		if(this.config.entity!=""){
			this.ativarOpacao(this.btSave);
			this.setModoNormal();
			var tela = this.htmlX.parents(".windowC")[0]["dta"];
			var gridd= this.config.grid!=""?tela[this.config.grid]:null;
			var itemS = {};		
			var idVl = tela[this.config.dbmap[0].field].getValue();	
			var actSave = this.getConfigAct("add");
			
			
			var idColumn = this.config.dbmap[0].column;
			var fnSelect = function(){};
			if(idVl!=""){				
				actSave = this.getConfigAct("edit");				
				//itemS = this.sync(gridd,tela);
				this.sync(gridd,tela);
				itemS = gridd.getSelectedItem(false);
			}else{
				//novo
				
				$.each(this.config.dbmap, function(index, value) {
					var campoF = value["field"];
					if(value.column!=idColumn){
						if(campoF.indexOf(".desc") > -1){
							var campoF = campoF.substring(0,campoF.indexOf(".desc"));
							itemS[value.column]=tela[campoF].getDesc();
						}else{
							itemS[value.column]=tela[campoF].getValue();
						}
					}					
				});
				
				//itemS[idColumn]=0;
				
				var idcolumnfield = this.config.dbmap[0].field;
				fnSelect = function(dta){				
					var idNext = dta.rs;			
					itemS[idColumn] = idNext;
					
					tela[idcolumnfield].setValue(idNext);
	                if(gridd!=null){
	                	gridd.addItem(itemS);        			
	                }
				};
				
			}
			if(this.config.entity!=""){	
				if(actSave["service"]!=""){
					var mybojS = $.extend(false,{},itemS);
					mybojS["s"] = actSave["service"];
		            mybojS["url"] = actSave["url"];
		            mybojS["format"] = actSave["format"];		            
					mybojS["t"] = this.uid;
					mybojS["onLoad"]=fnSelect;
					mybojS["puid"] = actSave["puid"];
					if(actSave["puid"]!=""){
						mybojS["puid"] = actSave["puid"];
					}	
					
		            var tmparms = actSave["params"].length;
		            if(tmparms > 0){
		            	if(tmparms > 1){
		            		mybojS["p"] = actSave["params"];
		            	}else{
		            		mybojS["p"] = actSave["params"][0];
		            	}	            	 
		            }
					rm.addRequest(mybojS);            
				}  
				//gridd.refresh();
			}
		}
	}
};

ToolBar.prototype.setGridChangeEventOnce = function(){
    if(this.config.grid!=""&&(!this.changeE)){
            this.changeE=true;
            var tela = this.htmlX.parents(".windowC")[0]["dta"];
            var gridd = tela[this.config.grid];
            gridd.itemChange = $.proxy(this.changeGrid,this);
    }
};

ToolBar.prototype.reload = function(){
	if(!this.btReload.htmlX.hasClass("Disabled")){
		this.setModoNormal();	
		this.ativarOpacao(this.btReload);
		var tela = this.htmlX.parents(".windowC")[0]["dta"];
		if(this.config.grid!=""){
		  var gridd = tela[this.config.grid];
         
		  //this.config["puid"] = tela.uid;
         
          if(!gridd.htmlX.hasClass("alredyEditableGrid")){ 
        	  gridd.htmlX.addClass("alredyEditableGrid");
        	  this.setGridChangeEventOnce();
         	}  
			
		}
		//alert(this.config.reload);
		var actPrint = this.getConfigAct("reload");
        if(actPrint["service"] != ""){		
		    
			var mybojS = {"s":actPrint["service"],"url":actPrint["url"],"format":actPrint["format"],"t":this.uid,"onLoad" : function(dta) {  
	            var toolbarD = $("#uid_"+dta.t);                    
	            var gridname = toolbarD[0]["dta"].config.grid;
	            if(gridname!=""){
	            	var tela2 = toolbarD.parents(".windowC")[0]["dta"];
	                var gridd1 = tela2[gridname];
	                gridd1.setDataProvider(dta.rs);
	                //gridd1.refresh();
	                //if(gridd1.dataProvider.length>0){
	                gridd1.htmlX.find("tbody tr:first").click();
	                //}
	            }
            }};
			if(actPrint["puid"]!=""){
				mybojS["puid"] = actPrint["puid"];
			}
            rm.addRequest(mybojS);			
		}else{	
			//gridd.refresh();
            //gridd.htmlX.find(".boxLines .cellgrid:first").click();
		}
	}
};

ToolBar.prototype.changeGrid = function(){
	if(this.config.grid!=""){
		var tela = this.htmlX.parents(".windowC")[0]["dta"];
		if(tela[this.config.grid].getDataProvider().length > 0){
			var itemSelected = tela[this.config.grid].getSelectedItem(false);	
			if(itemSelected){
				$.each(this.config.dbmap, function(index, value) {
					if(!value["fixed"] && value["field"].indexOf(".desc") < 0){						
						tela[value.field].setValue(itemSelected[value.column]);
					};					
				});				
				$.each(this._assoc, function(index, value) {
					value["p"] = [itemSelected[value["column"]]];
					_.loadModule(value);					
				});			
			}
		}
	}
};

function DataAbstract(configp){
	this.config = {};
	if(configp.entity!=""){
		//eh obrigatorio o entity para uso dos services
		this.config = $.extend({"idEntity":"id"+configp.entity,"add":configp.entity+".add","del":configp.entity+".excluir","edit":configp.entity+".editar","reload":configp.entity+".get"},configp);	
	}
	this.changeE = false;	
}

DataAbstract.prototype.getObjet=function(removeKey){
    var itemS = {};
    var idColumn = -1;
    if(removeKey){
        idColumn = this.config.dbmap[0].column;
    }
    var tela = $("#uid_"+this.config.uid)[0]["dta"];
    $.each(this.config.dbmap, function(index, value) {
        if(value.column!=idColumn){
		itemS[value.column]=tela[value.field].getValue();
	}
    });
    return itemS;
};
DataAbstract.prototype.setObjet=function(pobj){
    var tela = $("#uid_"+this.config.uid)[0]["dta"];
    $.each(this.config.dbmap, function(index, value){
		tela[value.field].setValue(pobj[value.column]);
    });
    return true;
};

DataAbstract.prototype.getService=function(pconfi){	
	var stmp = $.extend({"s":this.config.entity+".get","t":this.config.uid,"onLoad":function(dta){
        if(dta.rs.length > 0){
        	var row = $("#uid_"+dta.t)[0]["dta"]; 
        	row.dtaAbs.setObjet(dta.rs[0]); 
        }
	}},pconfi);	
	rm.addRequest(stmp);   
};

DataAbstract.prototype.add = function(){
    if(this.config.add!=""){				
        var mybojS = $.extend({},this.getObjet(true));
        mybojS["s"] = this.config.add;
        rm.addRequest(mybojS);
    }   
};

DataAbstract.prototype.del = function(){
    if(this.config.del!=""){				
        var mybojS = $.extend({},this.getObjet(false));
        mybojS["s"] = this.config.del;
        rm.addRequest(mybojS);
    }	
};
DataAbstract.prototype.edit = function(){
    if(this.config.edit!=""){				
        var mybojS = $.extend({},this.getObjet(false));
        mybojS["s"] = this.config.edit;
        rm.addRequest(mybojS);
    }	
};